<?php
namespace sms\ucpass;

/**
* 云之讯拓展类库
*/
class Ucpass
{
	protected $url;
	protected $appid;
	protected $templateid;
	protected $uid;
	protected $sid;
	protected $token;
	protected $yzm;
	protected $phone;

	public function __construct(){}

	public function submit($data){
		if (!is_array($data)) exit;
		// 重新配置
		$this->_config($data);
		// 返回发信结果
		return $this->SendSms($this->appid,$this->templateid,$this->param,$this->phone,$this->uid);
	}

	public function config($data){
		$this->_config();
	}

    // 重新定义配置
    private function _config($data){
    	if (!is_array($data)) exit;
		$this->sid = isset($data['sid']) ? $data['sid'] : NUll;
		$this->uid = isset($data['uid']) ? $data['uid'] : '';
		$this->param = isset($data['param']) ? $data['param'] : '';
		$this->templateid = isset($data['templateid']) ? $data['templateid'] : '';
		$this->phone = isset($data['phone']) ? $data['phone'] : '';
		$this->token = isset($data['token']) ? $data['token'] : NUll;
		$this->appid = isset($data['appid']) ? $data['appid'] : NUll;
		$this->templateid = isset($data['templateid']) ? $data['templateid'] : NUll;
		$this->url = isset($data['url']) ? $data['url'] : 'https://open.ucpaas.com/ol/sms/';
    }

    private function getResult($url, $body = null, $method)
    {
        $data = $this->connection($url,$body,$method);
        if (isset($data) && !empty($data)) {
            $result = $data;
        } else {
            $result = '没有返回数据';
        }
        return $result;
    }

    /**
     * @param $url    请求链接
     * @param $body   post数据
     * @param $method post或get
     * @return mixed|string
     */
	 
    private function connection($url, $body,$method)
    {
        if (function_exists("curl_init")) {
            $header = array(
                'Accept:application/json',
                'Content-Type:application/json;charset=utf-8',
            );
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            if($method == 'post'){
                curl_setopt($ch,CURLOPT_POST,1);
                curl_setopt($ch,CURLOPT_POSTFIELDS,$body);
            }
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            $result = curl_exec($ch);
            curl_close($ch);
        } else {
            $opts = array();
            $opts['http'] = array();
            $headers = array(
                "method" => strtoupper($method),
            );
            $headers[]= 'Accept:application/json';
            $headers['header'] = array();
            $headers['header'][]= 'Content-Type:application/json;charset=utf-8';

            if(!empty($body)) {
                $headers['header'][]= 'Content-Length:'.strlen($body);
                $headers['content']= $body;
            }

            $opts['http'] = $headers;
            $result = file_get_contents($url, false, stream_context_create($opts));
        }
        return $result;
    }

    /**
	单条发送短信的function，适用于注册/找回密码/认证/操作提醒等单个用户单条短信的发送场景
     * @param $appid        应用ID
     * @param $mobile       接收短信的手机号码
     * @param $templateid   短信模板，可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
     * @param null $param   变量参数，多个参数使用英文逗号隔开（如：param=“a,b,c”）
     * @param $uid			用于贵司标识短信的参数，按需选填。
     * @return mixed|string 
     * @throws Exception
     */
    private function SendSms($appid,$templateid,$param=null,$mobile,$uid){
        $url = $this->url . 'sendsms';
        $body_json = array(
            'sid'=>$this->sid,
            'token'=>$this->token,
            'appid'=>$appid,
            'templateid'=>$templateid,
			'param'=>$param,
			'mobile'=>$mobile,
			'uid'=>$uid,
        );
        $body = json_encode($body_json);
        $data = $this->getResult($url, $body,'post');
        return $data;
    }
	
	 /**
	 群发送短信的function，适用于运营/告警/批量通知等多用户的发送场景
     * @param $appid        应用ID
     * @param $mobileList   接收短信的手机号码，多个号码将用英文逗号隔开，如“18088888888,15055555555,13100000000”
     * @param $templateid   短信模板，可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
     * @param null $param   变量参数，多个参数使用英文逗号隔开（如：param=“a,b,c”）
     * @param $uid			用于贵司标识短信的参数，按需选填。
     * @return mixed|string 
     * @throws Exception
     */
	public function SendSms_Batch($appid,$templateid,$param=null,$mobileList,$uid){
        $url = $this->url . 'sendsms_batch';
        $body_json = array(
            'sid'=>$this->sid,
            'token'=>$this->token,
            'appid'=>$appid,
            'templateid'=>$templateid,
			'param'=>$param,
			'mobile'=>$mobileList,
			'uid'=>$uid,
        );
        $body = json_encode($body_json);
        $data = $this->getResult($url, $body,'post');
        return $data;
    }
}